package org.example;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class modifyTest {
    public static void main(String[] args) throws
            ClassNotFoundException,
            NoSuchFieldException,
            NoSuchMethodException,
            IllegalAccessException,
            InvocationTargetException,
            InstantiationException {
    Class bClass=Class.forName("org.example.User");
//    1构造真实类对象
    User boy=(User)bClass.newInstance();

        Method setName=bClass.getDeclaredMethod("setName", String.class);
//     2执行方法
        setName.invoke(boy,"zhansan");
        System.out.println(boy.getName());

        Field age=bClass.getDeclaredField("age");
//        3属性可被修改
        age.setAccessible(true);
//        4设置属性
        age.set(boy,18);
        System.out.println(boy.getAge());
    }
}
